package com.kucun.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.omg.CORBA.INTERNAL;
import org.sdlc.com.database.BaseDaoInterface;
import org.sdlc.com.database.bean.CglibDynaBean;
import org.sdlc.com.database.impl.MysqlBaseDaoImpl;
import org.sdlc.com.database.impl.OracleBaseDaoImpl;

import com.google.gson.JsonArray;
import com.kucun.bean.Repertory;

public class TreeNodeService {
	/*
	*serviceImpl 业务实现
	
	public List<JsonTreeData> getGoodsSpec(HtSpecifications spec) {
	        List<HtSpecifications> resultList = htSpecificationsDaoImpl.findAll("getSpecList", spec);
	        List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
	         	为了整理成公用的方法，所以将查询结果进行二次转换。
	          * 其中specid为主键ID，varchar类型UUID生成
	          * parentid为父ID
	          * specname为节点名称
	          * 
	        for (htSpecifications htSpecifications : resultList) {
	            JsonTreeData treeData = new JsonTreeData();
	            treeData.setId(htSpecifications.getSpecid());
	            treeData.setPid(htSpecifications.getParentid());
	            treeData.setText(htSpecifications.getSpecname());
	            treeDataList.add(treeData);
	        }
	        //最后得到结果集,经过FirstJSON转换后就可得所需的json格式
	        List<JsonTreeData> newTreeDataList = TreeNodeUtil.getfatherNode(treeDataList);
	        return newTreeDataList;
	    }*/
	public static void main(String[] args) {
		BaseDaoInterface basedao=MysqlBaseDaoImpl.getInstance();
		StringBuffer sb=new StringBuffer();
		List<Map<String, Object>> s=new TreeNodeService().doTreeData(1,basedao,sb);
		System.out.println(sb);
		//new TreeNodeService().test();
	}
	public void test(){
		BaseDaoInterface basedao=MysqlBaseDaoImpl.getInstance();
		List<CglibDynaBean> lists=null;
		lists= basedao.getListCglib("select * from repertory where parentId="+1);
		 List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
		 for (CglibDynaBean cglib : lists) {
			 	JsonTreeData treeData = new JsonTreeData();
	            treeData.setId(cglib.getValue("id").toString());
	            treeData.setPid(cglib.getValue("parentid")!=null?cglib.getValue("parentid").toString():null);
	           // treeData.setText(cglib.getValue("text").toString());
	            treeDataList.add(treeData);
		 }
		 System.out.println("treeDataList.size="+treeDataList.size());
		 List<JsonTreeData> newTreeDataList = TreeNodeUtil.getfatherNode(treeDataList);
	     System.out.println(newTreeDataList.size()); 
	     System.out.println(new GsonUtils().ChagneDateFormat(newTreeDataList));
	}
	
	public List<Map<String, Object>> doTreeData(Integer parentid,BaseDaoInterface basedao,StringBuffer sb) {
		List<Repertory> list=basedao.getListBySql("select id as isdelet,titles,parentid from repertory where parentId="+parentid, Repertory.class,null);//查询数据库，查出parent_level=parentid的所有数据
		ArrayList<Map<String, Object>> newlist=new ArrayList<Map<String, Object>>();
		for(int i=0;i<list.size();i++){
		 HashMap<String, Object> map=new HashMap<String, Object>();
		 map.put("id", list.get(i).getIsdelet());
		 map.put("name", list.get(i).getTitles());
		 map.put("parentid", list.get(i).getParentid());
		 sb.append(list.get(i).getIsdelet()+",");
		 Integer node=list.get(i).getIsdelet();
		 CglibDynaBean count=basedao.getSingleCglib("select count(*) as isdelet from repertory where parentId="+node,null);
		 System.out.println((Long)count.getValue("isdelet")+"=========="+node);
		 if((Long)count.getValue("isdelet") >0){
		 //该节点下有子节点
		 List<Map<String, Object>> listchild=this.doTreeData(node,basedao,sb);
		 map.put("children", listchild);
		 map.put("state", "closed");
		 }else{
		 //无子节点
		 }	 
		 newlist.add(map);	 
		}
		return newlist;
		}
}
